// rules for fields
/**
 * Project Name
- min length 5
- max length 256
- unique
Project Description
- required
- max length 4000
Goal
- required
- max length 256
Mission
- required
- max length 256
Photo
- required
- max file size 1MB
Video Link
- required
- max length 1024
Expected budget
- required
- min value 10$
- max value 100 000 000 000
- numeric with 0-2 decimal digits
Expiration Date
- required
- date format
- greater than now
- max value now + 6 months
 */

$(document).ready(function(){
	// $('#validationBtn').click(function(){
	// 	validation();
	// });
	
	$("#regprojectForm").on('submit', function(event){
		event.preventDefault();

		if(validation()){
			//alert('Validation fail');
			return false;
		}

		this.submit();
		
		// if($('#regprojectForm').data('errors')){
		
		// 	alert('Validation fail');
		// 	return false;
		// }
		
		// return false;
	});
});

var regex = {
		Name: /.{5,256}/,
		description: /.{4,4000}/, 
		goal: /.{5,256}/,
		mission: /.{5,256}/,
		videoLink: /^(http:\/\/)(www\.)?(youtube\.com).{5,1024}/,
		expectedBudget: /[0-9]{2,11}(\.[0-9]){0,2}/,
		expireDate: /[0-9]{4}-[0-9]{2}-[0-9]{2}/,
		photo: /.*/
// 		
		// email: /^(\S+)@([a-z0-9-]+)(\.)([a-z]{2,4})(\.?)([a-z]{0,4})+$/, //EMAIL
		// info: /.{4,}/, //Інфо - поле
		// phone: /^(\()[0-9]{3}(\))\s[0-9]{3}\-[0-9]{4}$/, //Телефон
		// integer: /[0-9]+/
	}
	
	
var error_messages = {
	Name: "Name must be more than 5 symbols and less than 256",
	description: "Description must be longer than 4 symbols (and less than 4000)", 
	goal: "Goal must be more than 5 but less than 256 symbols",
	mission: "Mission must be more than 5 but less than 256 symbols",
	videoLink: "Videolink must look like http://youtube.com/<i>12345</i>",
	expectedBudget: "More or equal to 10.00",
	expireDate: "Expiration date must be not earlier than +30 days from current date " + moment().add('days', 30).calendar()
};
	
	function specificTester(inputName) {
	
		switch(inputName) {
			case 'Name':
				return regex['Name'];
			case 'goal':
				return regex['goal'];
			case 'mission':
				return regex['mission'];
			case 'videoLink':
				return regex['videoLink'];
			case 'photo':
				return regex['photo'];
			case 'expectedBudget':
				return regex['expectedBudget'];
			case 'expireDate':
				return regex['expireDate'];	
			case 'description': 
				return regex['description'];	
			
			default: return 0;
		}
	}
	
	function fieldsFocusOut() {
		for(var i = 1; i <= fieldsetCount; ++i){
			
			$('#formElem').children(':nth-child('+ parseInt(i) +')').find(':input:not(button)').each(function(){
				$(this).focusout(function(){
					var valueLength = jQuery.trim($(this).val()).length;
					var tester = specificTester($(this).attr('name').match(/\[(.*?)\]/)[1]);
					if(!tester) return;
				
					if(!tester.test($(this).val())) $(this).css('background-color','#fff7f2');
				
					else $(this).css('background-color','#FFFFFF');
				});
			});
		}
	}
	
	function validation()
	{
		var error = 0;
		var hasError = false;
		
		// $('#regprojectForm').children(':nth-child('+ parseInt(step) +')').find(':input:not(button)').each(function(){
		$('#regprojectForm').children().find(':input:not(button):not(:last-child)').each(function(){
			var $this 		= $(this);
			var valueLength = jQuery.trim($this.val()).length;
			// var elname = ($this.attr('name').match(/\[(.*?)\]/)[1]);
			var elname = ($this.attr('name'));
			//alert(elname);

			// var tester = specificTester($this.attr('name').match(/\[(.*?)\]/)[1]);
			var tester = specificTester($this.attr('name'));
			if(!tester) {
				hasError = true;
				$('#regprojectForm').data('errors', hasError);
				return 1;
			}
			
			if(!tester.test($this.val())){
				hasError = true;
				
				if(elname == 'expireDate')
				{
					// var now = moment.format();
					//var formdate = moment($this.val());
					// var futuredate = moment().add('days', 30).calendar();
// 					
					// alert(now);
					//alert(formdate);
// 
					// alert(futuredate);
					// // moment("2011-10-10", "YYYY-MM-DD").isValid();

					// alert(moment($this.val()));
				}					
			}
			else{
				hasError = false;
				if(elname == 'expireDate')
				{
					hasError = checkExpirationDate($this.val());					
				}				
			}
			
			if(hasError){
				$this.css('background-color','#fff7f2');
				$this.parent().parent().children().find('.error_tooltip').html(error_messages[elname]);
			}else{
				$this.css('background-color','#FFFFFF');
				$this.parent().parent().children().find('.error_tooltip').html("");
			}
			
			//-----------------------------------------
		});
		
		if($("#uniqproj").val() == "1") {
			hasError = true;
			$("#projectname").parent().parent().children().find('.error_tooltip').html("Project name must be unique!");
		}
		
		if(hasError){
			error = 1;
		}
				
		$('#regprojectForm').data('errors', hasError);
		
		return error;
	}
	
	function checkExpirationDate(expireDate)
	{	
		if(!moment(expireDate).isValid()){
			return true;
		}
		
		var now = moment().format('M/D/YYYY');
		var formdate = moment(expireDate);//.format('M/D/YYYY');
		var futuredate = moment().add('days', 30).calendar();
		var difference = formdate.diff(futuredate,'days');
		
//		alert('Difference: ' + difference)

		if(difference < 0)
			return true;

		//alert(now);
		// alert('From form: ' + formdate);
		// alert('JS future: ' + futuredate);
		// alert('Difference: ' + difference)

		// moment("2011-10-10", "YYYY-MM-DD").isValid();
		// alert(moment($this.val()));
		
		return false;
	}
